package com.pkx.guide.info.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pkx.guide.common.annotation.Log;
import com.pkx.guide.common.annotation.ORpose;
import com.pkx.guide.common.enums.BusinessType;
import com.pkx.guide.info.entity.TInfoGuidePublish;
import com.pkx.guide.info.service.TInfoGuidePublishService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 诱导屏发布信息表 控制器
 *
 * @author sunsy
 * @since 2023-11-14
 */
@RestController
@RequestMapping("/info/publish")
@Tag(name = "诱导屏发布信息表")
@Slf4j
@ORpose
public class TInfoGuidePublishController {

    @Autowired
    private TInfoGuidePublishService tInfoGuidePublishService;

    @Operation(summary = "诱导屏发布信息表分页列表")
    @GetMapping("/page")
    public IPage<TInfoGuidePublish> page(Page page, TInfoGuidePublish tInfoGuidePublish) {
        return tInfoGuidePublishService.pageList(page, tInfoGuidePublish);
    }

    @Operation(summary = "诱导屏发布信息表列表")
    @GetMapping("/list")
    public List<TInfoGuidePublish> list(TInfoGuidePublish tInfoGuidePublish) {
        return tInfoGuidePublishService.listByEntity(tInfoGuidePublish);
    }

    @Operation(summary = "诱导屏发布信息表详情")
    @GetMapping(value = "/{id}")
    public TInfoGuidePublish selectById(@PathVariable("id") Integer id) {
        return tInfoGuidePublishService.selectById(id);
    }

    @Operation(summary = "诱导屏发布信息表新增")
    @PostMapping("/")
    @Log(title = "诱导屏发布信息表", businessType = BusinessType.INSERT)
    public Boolean add(@RequestBody TInfoGuidePublish tInfoGuidePublish) throws Exception {


//            CompletableFuture<String> future = new CompletableFuture<>();
//            ProducerQest qest = new ProducerQest();
//            JSONArray originalPublishDetail = JSONArray.parseArray(tInfoGuidePublish.getPublishDetail());
//            qest.setIpList(originalPublishDetail.toJSONString());
//            qest.setIpListSize(originalPublishDetail.size());
//            qest.setNeedReturnResponse(true);
//            qest.setCompletionFuture(future);
//            dataQueue.put( qest);
//            // 等待处理结果
//        try {
//            String result = future.get(30, TimeUnit.SECONDS);
////        return ResponseEntity.ok(result).hasBody();
//            log.info("返回结果：{}", result);
//        }catch (Exception e){
//            log.error("异常：{}", e.getMessage());
//        }
        return tInfoGuidePublishService.saveTInfoGuidePublish(tInfoGuidePublish);
    }

    @Operation(summary = "批量保存")
    @PostMapping("/saveBatchGuidePublish")
    @Log(title = "批量保存", businessType = BusinessType.INSERT)
    public Boolean saveBatchGuidePublish(@RequestBody TInfoGuidePublish tInfoGuidePublish) throws Exception {
        return tInfoGuidePublishService.saveBatchGuidePublish(tInfoGuidePublish);
    }

    @Operation(summary = "诱导屏发布信息表修改")
    @PutMapping("/")
    @Log(title = "诱导屏发布信息表", businessType = BusinessType.UPDATE)
    public Boolean modify(@RequestBody TInfoGuidePublish tInfoGuidePublish) throws Exception {
        return tInfoGuidePublishService.modify(tInfoGuidePublish);
    }

    @Operation(summary = "停止自动执行")
    @PostMapping("/stopAutoPublish")
    @Log(title = "诱导屏发布信息表", businessType = BusinessType.UPDATE)
    public Boolean stopAutoPublish(@RequestBody TInfoGuidePublish tInfoGuidePublish) {
        return tInfoGuidePublishService.stopAutoPublish(tInfoGuidePublish);
    }

    @Operation(summary = "开启自动执行")
    @PostMapping("/startAutoPublish")
    @Log(title = "诱导屏发布信息表", businessType = BusinessType.UPDATE)
    public Boolean startAutoPublish(@RequestBody TInfoGuidePublish tInfoGuidePublish) {
        return tInfoGuidePublishService.startAutoPublish(tInfoGuidePublish);
    }

    @Operation(summary = "更改模式")
    @PostMapping("/updateMode")
    @Log(title = "诱导屏发布信息表", businessType = BusinessType.UPDATE)
    public Boolean updateMode(@RequestBody TInfoGuidePublish tInfoGuidePublish) throws Exception {
        return tInfoGuidePublishService.updateMode(tInfoGuidePublish);
    }

    @Operation(summary = "诱导屏发布信息表删除(单个条目)")
    @DeleteMapping(value = "/{id}")
    @Log(title = "诱导屏发布信息表", businessType = BusinessType.DELETE)
    public Boolean deleteById(@PathVariable("id") Integer id) {
        return tInfoGuidePublishService.deleteById(id);
    }

    @Operation(summary = "查询正在执行任务")
    @GetMapping(value = "/getExecutingPublish")
    public String getExecutingPublish() {
        return tInfoGuidePublishService.getExecutingPublish();
    }


//    /**
//     * 每月1号返回数据，其他时候返回空
//     */
//    @GetMapping("/monthlyData")
//    public Object getMonthlyData() {
//        // 获取当前日期
//        LocalDate today = LocalDate.now();
//
//        // 判断是否为每月1号
//        if (today.getDayOfMonth() == 1) {
//            // 1号：返回实际数据（这里模拟数据，实际项目中替换为业务逻辑）
//            List<TInfoGuidePublish> basePublishList = tInfoGuidePublishService.list(new LambdaQueryWrapper<TInfoGuidePublish>()
//                    .eq(TInfoGuidePublish::getDelFlg, 0)
//                    .eq(TInfoGuidePublish::getState, 0)
//                    .eq(TInfoGuidePublish::getModeType, 0)
//            );
//            return basePublishList;
//        } else {
//            return null;
//        }
//    }
//    private final BlockingQueue<ProducerQest> dataQueue = new ArrayBlockingQueue<>(1);
//

}
